package com.bsoft.gol.hcs.specialist.service.impl;

import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.bean.UserVo;
import hcn.specialist.FusGroupChronicRecordExtendProperty;
import com.bsoft.gol.hcs.odshmrecord.dao.OdsHmRecordDAO;
import hcn.sync.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import service.rpc.impl.BaseUserServiceImpl;
import com.bsoft.gol.hcs.specialist.bean.request.GroupChronicDeleteRequest;
import com.bsoft.gol.hcs.specialist.bean.request.GroupChronicRecordExtendRequest;
import com.bsoft.gol.hcs.specialist.bean.request.GroupChronicRecordQo;
import com.bsoft.gol.hcs.specialist.bean.request.GroupChronicRecordStagesRequest;
import com.bsoft.gol.hcs.specialist.bean.response.*;
import com.bsoft.gol.hcs.specialist.dao.*;
import com.bsoft.gol.hcs.specialist.entity.DiseaseExtendPropertyEntity;
import com.bsoft.gol.hcs.specialist.entity.FusGroupChronicRecordStagesProperty;
import com.bsoft.gol.hcs.specialist.service.IPatientViewService;
import com.bsoft.gol.hcs.utils.BeanUtil;
import com.bsoft.gol.hcs.utils.CommonUtils;
import com.bsoft.gol.hcs.utils.DateUtil;
import com.bsoft.gol.hcs.visitrecord.dao.VrInpatientVisitRecordDAO;
import com.bsoft.gol.hcs.visitrecord.dao.VrOutpatientVisitRecordDAO;
import com.bsoft.gol.hcs.visitrecord.dao.VrSurgicalRecordDAO;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 患者视图service实现类
 *
 * @author lanyu
 * @date 2021年10月21日 15:53
 */
@SsdevService("patientViewService")
public class PatientViewServiceImpl implements IPatientViewService {

    @Autowired
    private GroupRecordExtendPropertyDAO groupRecordExtendPropertyDAO;

    @Autowired
    private GroupRecordStagesPropertyDAO groupRecordStagesPropertyDAO;

    @Autowired
    private FusAnswerInHospitalInfoDAO fusAnswerInHospitalInfoDAO;

    @Autowired
    private FusAnswerOutpatientInfoDAO fusAnswerOutpatientInfoDAO;

    @Autowired
    private FusAnswerOperativeInfoDAO fusAnswerOperativeInfoDAO;

    @Autowired
    private FusAnswerTreatmentInfoDAO fusAnswerTreatmentInfoDAO;

    @Autowired
    private FusAnswerDrugInfoDAO fusAnswerDrugInfoDAO;

    @Autowired
    private OdsHmRecordDAO odsHmRecordDAO;

    @Autowired
    private FollowUpRecordsDAO followUpRecordsDAO;

    @Autowired
    private FusAnswerInspectionReportInfoDAO fusAnswerInspectionReportInfoDAO;

    @Autowired
    private FusAnswerCheckReportInfoDAO fusAnswerCheckReportInfoDAO;

    @Autowired
    private DiseaseExtendPropertyDAO diseaseExtendPropertyDAO;

    @Autowired
    private BaseUserServiceImpl baseUserService;

    @Autowired
    private DiseaseSpeciesRelateDAO diseaseSpeciesRelateDAO;

    @Autowired
    private PrescriptionRecordDAO prescriptionRecordDAO;

    @Autowired
    private VrInpatientVisitRecordDAO vrInpatientVisitRecordDAO;

    @Autowired
    private VrOutpatientVisitRecordDAO vrOutpatientVisitRecordDAO;

    @Autowired
    private TreatmentRecordDAO treatmentRecordDAO;

    @Autowired
    private VrSurgicalRecordDAO vrSurgicalRecordDAO;

    @Autowired
    private InspectReportRecordDAO inspectReportRecordDAO;

    @Autowired
    private ExamReportDAO examReportDAO;

    /**
     * 根据病种id获取扩展属性信息
     *
     * @param qo
     * @return
     */
    @Override
    @RpcService
    public List<GroupExtendPropertyResponse> queryGroupRecordExtendList(GroupChronicRecordQo qo) throws ControllerException {
        if (CommonUtils.isEmpty(qo.getDiseaseTypeId())) {
            throw new ControllerException("病种id不可为空");
        }
        List<GroupExtendPropertyResponse> groupExtendPropertyResponses = groupRecordExtendPropertyDAO.queryFirstGroupExtendPropertyList(qo);
        if (CommonUtils.isNotEmpty(groupExtendPropertyResponses)) {
            for (GroupExtendPropertyResponse groupExtendProperty : groupExtendPropertyResponses) {
                List<GroupExtendPropertyListResponse> groupExtendPropertyInfos = groupRecordExtendPropertyDAO.querySecondGroupExtendPropertyList(qo, groupExtendProperty.getExtendId());
                if (CommonUtils.isNotEmpty(groupExtendPropertyInfos)) {
                    for (GroupExtendPropertyListResponse groupExtendPropertyResponse : groupExtendPropertyInfos) {
                        if (CommonUtils.isNotEmpty(groupExtendPropertyResponse.getCreateUserId())) {
                            UserVo userVo = baseUserService.getUserById(groupExtendPropertyResponse.getCreateUserId());
                            groupExtendPropertyResponse.setCreateUser(userVo.getUserName());
                        }
                    }
                }
                groupExtendProperty.setPropertyChildList(groupExtendPropertyInfos);
            }
        }
        return groupExtendPropertyResponses;
    }

    /**
     * 根据入组记录id和病种id获取分期属性信息
     *
     * @param qo
     * @return
     */
    @Override
    @RpcService
    public List<GroupStagesListResponse> queryGroupRecordStagesList(GroupChronicRecordQo qo) throws ControllerException {
        if (CommonUtils.isEmpty(qo.getDiseaseTypeId())) {
            throw new ControllerException("病种id不可为空");
        }
        List<GroupStagesListResponse> groupStagesListResponses = groupRecordStagesPropertyDAO.querySecondGroupStagesList(qo);
        if (CommonUtils.isNotEmpty(groupStagesListResponses)) {
            for (GroupStagesListResponse groupStagesListResponse : groupStagesListResponses) {
                if (CommonUtils.isNotEmpty(groupStagesListResponse.getCreateUserId())) {
                    UserVo userVo = baseUserService.getUserById(groupStagesListResponse.getCreateUserId());
                    groupStagesListResponse.setCreateUser(userVo.getUserName());
                }
            }
        }
        return groupStagesListResponses;
    }

    /**
     * 保存分期属性信息
     *
     * @param request
     */
    @Override
    @RpcService
    public void saveOrUpdateStages(GroupChronicRecordStagesRequest request) throws ControllerException {
        FusGroupChronicRecordStagesProperty property = new FusGroupChronicRecordStagesProperty();
        BeanUtil.copyProperties(request, property);
        property.setCreateTime(new Date());
        property.setStagesTime(CommonUtils.isEmpty(request.getStagesTime()) ? null : DateUtil.getDateTime("yyyy-MM-dd HH:mm:ss", request.getStagesTime()));
        //获取当前登录用户id
        UserVo userVo = baseUserService.getUserIdByOnline();
        if (CommonUtils.isNotEmpty(userVo)) {
            property.setCreateUserId(userVo.getUserId());
        }
        groupRecordStagesPropertyDAO.save(property);
    }

    /**
     * 保存扩展属性信息
     *
     * @param request
     */
    @Override
    @RpcService
    public void saveExtendProperty(GroupChronicRecordExtendRequest request) throws ControllerException {
        FusGroupChronicRecordExtendProperty property = new FusGroupChronicRecordExtendProperty();
        BeanUtil.copyProperties(request, property);
        property.setCreateTime(new Date());
        property.setExtendTime(CommonUtils.isEmpty(request.getExtendTime()) ? null : DateUtil.getDateTime("yyyy-MM-dd HH:mm:ss", request.getExtendTime()));
        //获取当前登录用户id
        UserVo userVo = baseUserService.getUserIdByOnline();
        if (CommonUtils.isNotEmpty(userVo)) {
            property.setCreateUserId(userVo.getUserId());
        }
        groupRecordExtendPropertyDAO.save(property);
    }

    /**
     * 删除扩展属性
     *
     * @param request
     */
    @Override
    @RpcService
    public void deleteExtendProperty(GroupChronicDeleteRequest request) throws ControllerException {
        if (CommonUtils.isEmpty(request.getGroupChronicRecordId())) {
            throw new ControllerException("扩展属性记录id不可为空");
        }
        groupRecordExtendPropertyDAO.deleteExtendPropertyByExtendId(request.getGroupChronicRecordId());
    }

    /**
     * 删除分期
     *
     * @param request
     */
    @Override
    @RpcService
    public void deleteStages(GroupChronicDeleteRequest request) throws ControllerException {
        if (CommonUtils.isEmpty(request.getStagesRecordId())) {
            throw new ControllerException("分期id不可为空");
        }
        groupRecordStagesPropertyDAO.deleteStagesByStagesId(request.getStagesRecordId());
    }


    /**
     * 获取治疗信息
     *
     * @param qo
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public List<TreatmentRecordResponse> queryTreatmentRecord(GroupChronicRecordQo qo) throws ControllerException {
        if (CommonUtils.isEmpty(qo.getPatientId())) {
            throw new ControllerException("患者唯一标识不可为空");
        }
        if (CommonUtils.isEmpty(qo.getDiseaseTypeId())) {
            throw new ControllerException("病种id不可为空");
        }
        //his同步的治疗信息
        List<TreatmentRecordResponse> responseList = treatmentRecordDAO.queryTreatmentInfoByIdCard(qo);
        if (CommonUtils.isNotEmpty(responseList)) {
            for (TreatmentRecordResponse treatmentRecord : responseList) {
                List<TreatmentTimeInfoResponse> treatmentTimeInfos = treatmentRecordDAO.queryTreatmentTimeListByIdCard(qo, treatmentRecord.getTreatmentCode());
                treatmentRecord.setTreatmentTimeList(treatmentTimeInfos);
            }
        }
        //编辑器同步的治疗信息
        List<TreatmentRecordResponse> treatmentRecordResponses = fusAnswerTreatmentInfoDAO.queryAnswerTreatmentInfoList(qo);
        if (CommonUtils.isNotEmpty(treatmentRecordResponses)) {
            for (TreatmentRecordResponse treatmentRecord : treatmentRecordResponses) {
                List<TreatmentTimeInfoResponse> treatmentTimeInfos = fusAnswerTreatmentInfoDAO.queryAnswerTreatmentTimeList(qo, treatmentRecord.getTreatmentName());
                treatmentRecord.setTreatmentTimeList(treatmentTimeInfos);
            }
        }
        //完整的治疗信息
        responseList.addAll(treatmentRecordResponses);
        //病种配置的关联治疗信息
        List<TreatmentRecordResponse> treatmentRecordResponses1 = diseaseSpeciesRelateDAO.queryTreatmentInfo(qo);
        //合并治疗信息
        List<TreatmentRecordResponse> resultTreatments = new ArrayList<>();
        if (CommonUtils.isEmpty(treatmentRecordResponses1)) {
            return resultTreatments;
        }
        for (TreatmentRecordResponse t : treatmentRecordResponses1) {
            for (TreatmentRecordResponse response : responseList) {
                if (response.getTreatmentName().equals(t.getTreatmentName())) {
                    TreatmentRecordResponse recordResponse = new TreatmentRecordResponse();
                    BeanUtil.copyProperties(response, recordResponse);
                    t.setTreatmentTimeList(response.getTreatmentTimeList());
                }
            }
        }
        List<TreatmentRecordResponse> collect = treatmentRecordResponses1.stream().map(treatment -> {
            List<TreatmentTimeInfoResponse> timeList = new ArrayList<>();
            if (CommonUtils.isEmpty(treatment.getTreatmentTimeList())) {
                treatment.setTreatmentTimeList(timeList);
            }
            return treatment;
        }).collect(Collectors.toList());
//        List<TreatmentRecordResponse> result = resultTreatments.stream()
//                // 表示name为key，接着如果有重复的，那么从MedicalRecordResponse对象o1与o2中筛选出一个，这里选择o1，
//                .collect(Collectors.toMap(TreatmentRecordResponse::getTreatmentName, a -> a, (o1,o2)-> {
//                    o1.getTreatmentTimeList().addAll(o2.getTreatmentTimeList());
//                    return o1;
//                })).values().stream().collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取手术信息
     *
     * @param qo
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public List<OperativeRecordResponse> queryOperativeRecord(GroupChronicRecordQo qo) throws ControllerException {
        if (CommonUtils.isEmpty(qo.getPatientId())) {
            throw new ControllerException("患者唯一标识不可为空");
        }
        if (CommonUtils.isEmpty(qo.getDiseaseTypeId())) {
            throw new ControllerException("病种id不可为空");
        }
        //获取病种配置时的关联手术信息
        List<OperativeRecordResponse> operativeRecordRelateList = diseaseSpeciesRelateDAO.queryRelateOperativeInfoList(qo);
        //编辑器同步的手术信息
        List<OperativeRecordResponse> operativeRecordList = fusAnswerOperativeInfoDAO.queryAnswerOperativeInfoList(qo);
        //his同步的手术信息
        List<OperativeRecordResponse> operativeRecordResponses = vrSurgicalRecordDAO.queryOperativeInfoListByIdCard(qo);
        operativeRecordList.addAll(operativeRecordResponses);
        List<OperativeRecordResponse> result = new ArrayList<>();
        //若病种配置的手术信息为空，则直接返回空集合
        if (CommonUtils.isEmpty(operativeRecordRelateList)) {
            return result;
        }
        for (OperativeRecordResponse operativeRecord : operativeRecordList) {
            //病种配置获取的手术信息
            for (OperativeRecordResponse o : operativeRecordRelateList) {
                if (operativeRecord.getOperativeName().equals(o.getOperativeName())) {
                    OperativeRecordResponse response = new OperativeRecordResponse();
                    BeanUtil.copyProperties(operativeRecord, response);
                    result.add(response);
                }
            }
        }
        return result;
    }

    /**
     * 获取药品信息
     *
     * @param qo
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public List<MedicalRecordResponse> queryMedicalRecord(GroupChronicRecordQo qo) throws ControllerException {
        if (CommonUtils.isEmpty(qo.getPatientId())) {
            throw new ControllerException("患者唯一标识不可为空");
        }
        if (CommonUtils.isEmpty(qo.getDiseaseTypeId())) {
            throw new ControllerException("病种id不可为空");
        }
        //his同步的用药记录
        List<MedicalRecordResponse> prescriptionList = prescriptionRecordDAO.queryPrescriptionRecordList(qo);
        if (CommonUtils.isNotEmpty(prescriptionList)) {
            for (MedicalRecordResponse prescription : prescriptionList) {
                List<MedicalTimeListResponse> prescriptionTimeList = prescriptionRecordDAO.queryAnswerMedicalTimeList(qo, prescription.getMedicalId());
                prescription.setMedicalTimeList(prescriptionTimeList);
            }
        }
        //表单编辑同步的用药信息
        List<MedicalRecordResponse> medicalRecordResponses = fusAnswerDrugInfoDAO.queryAnswerMedicalInfoList(qo);
        if (CommonUtils.isNotEmpty(medicalRecordResponses)) {
            for (MedicalRecordResponse medicalRecord : medicalRecordResponses) {
                List<MedicalTimeListResponse> medicalTimeLists = fusAnswerDrugInfoDAO.queryAnswerMedicalTimeList(qo, medicalRecord.getMedicalName());
                medicalRecord.setMedicalTimeList(medicalTimeLists);
            }
        }
        //medicalRecordResponses.addAll(prescriptionList);
        //病种配置的用药信息
        List<MedicalRecordResponse> medicalRecordResponses1 = diseaseSpeciesRelateDAO.queryRelateMedicalInfoList(qo);
        //合并用药信息
        List<MedicalRecordResponse> medicalRecordResponses2 = new ArrayList<>();
        if (CommonUtils.isEmpty(medicalRecordResponses1)) {
            return medicalRecordResponses1;
        }
        for (MedicalRecordResponse medicalRecordResponse : medicalRecordResponses1) {
            //his同步的用药信息
            for (MedicalRecordResponse m1 : prescriptionList) {
                if (medicalRecordResponse.getMedicalName().equals(m1.getMedicalName())) {
                    medicalRecordResponse.setMedicalTimeList(m1.getMedicalTimeList());
                }
            }
            //表单编辑器同步的用药信息
            for (MedicalRecordResponse prescription : medicalRecordResponses) {
                if (medicalRecordResponse.getMedicalName().equals(prescription.getMedicalName())) {
                    medicalRecordResponse.setMedicalTimeList(prescription.getMedicalTimeList());
                }
            }
        }

        List<MedicalRecordResponse> collect = medicalRecordResponses1.stream().map(medical -> {
            List<MedicalTimeListResponse> timeList = new ArrayList<>();
            if (CommonUtils.isEmpty(medical.getMedicalTimeList())) {
                medical.setMedicalTimeList(timeList);
            }
            return medical;
        }).collect(Collectors.toList());
        //合并相同的用药结果
//        List<MedicalRecordResponse> result = medicalRecordResponses2.stream()
//                // 表示name为key，接着如果有重复的，那么从MedicalRecordResponse对象o1与o2中筛选出一个，这里选择o1，
//                .collect(Collectors.toMap(MedicalRecordResponse::getMedicalName, a -> a, (o1,o2)-> {
//                    o1.getMedicalTimeList().addAll(o2.getMedicalTimeList());
//                    return o1;
//                })).values().stream().collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取住院/门诊记录
     *
     * @param qo
     * @return
     */
    @Override
    @RpcService
    public InAndOutPatientInfoRecordList queryPatientInfoRecord(GroupChronicRecordQo qo) throws ControllerException {
        if (CommonUtils.isEmpty(qo.getPatientId())) {
            throw new ControllerException("患者唯一标识不可为空");
        }
        if (CommonUtils.isEmpty(qo.getDiseaseTypeId())) {
            throw new ControllerException("病种id不可为空");
        }
        InAndOutPatientInfoRecordList infoRecordList = new InAndOutPatientInfoRecordList();
        //编辑器同步的住院记录
        List<InPatientInfoListResponse> inPatientInfoLists = fusAnswerInHospitalInfoDAO.queryAnswerInPatientInfoList(qo);
        //从his同步过来的住院记录
        List<InPatientInfoListResponse> infoListResponses = vrInpatientVisitRecordDAO.queryInPatientInfoListByIdCard(qo);
        inPatientInfoLists.addAll(infoListResponses);
        //编辑器同步的门诊记录
        List<OutPatientInfoListResponse> outPatientInfoLists = fusAnswerOutpatientInfoDAO.queryAnswerOutPatientInfoList(qo);
        //从his同步过来的门诊记录
        List<OutPatientInfoListResponse> outPatientInfoResponses = vrOutpatientVisitRecordDAO.queryAnswerOutPatientInfoList(qo);
        outPatientInfoLists.addAll(outPatientInfoResponses);
        infoRecordList.setInPatientList(inPatientInfoLists);
        infoRecordList.setOutPatientList(outPatientInfoLists);
        return infoRecordList;
    }

    /**
     * 关联指标获取
     *
     * @param qo
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public List<CrucialRecordResponse> queryCrucialRecord(GroupChronicRecordQo qo) throws ControllerException {
        if (CommonUtils.isEmpty(qo.getMpiId())) {
            throw new ControllerException("患者mpiId不可为空");
        }
        if (CommonUtils.isEmpty(qo.getPatientId())) {
            throw new ControllerException("患者唯一标识不可为空");
        }
        if (CommonUtils.isEmpty(qo.getDiseaseTypeId())) {
            throw new ControllerException("病种id不可为空");
        }
        if (CommonUtils.isEmpty(qo.getBeginTime())) {
            throw new ControllerException("开始时间不能为空");
        }
        if (CommonUtils.isEmpty(qo.getEndTime())) {
            throw new ControllerException("结束时间不能为空");
        }
        //健康监测同步的关联指标信息
        List<CrucialRecordResponse> crucialRecordResponses = odsHmRecordDAO.queryAnswerCrucialInfoList(qo);
        if (CommonUtils.isNotEmpty(crucialRecordResponses)) {
            for (CrucialRecordResponse crucialRecord : crucialRecordResponses) {
                crucialRecord.setCrucialType("1");
                List<CrucialTimeRecordList> crucialTimeLists = odsHmRecordDAO.queryAnswerCrucialTimeList(qo, crucialRecord.getIndexType());
                crucialRecord.setCrucialTimeList(crucialTimeLists);
            }
        }
        //his同步的检验数据
        List<CrucialRecordResponse> crucialRecordResponses3 = inspectReportRecordDAO.queryInspectionReportInfo(qo);
        if (CommonUtils.isNotEmpty(crucialRecordResponses3)) {
            for (CrucialRecordResponse crucialRecord : crucialRecordResponses3) {
                crucialRecord.setCrucialType("1");
                List<CrucialTimeRecordList> crucialTimeLists = inspectReportRecordDAO.queryInspectionReportTimeInfo(qo, crucialRecord.getIndexType());
                crucialRecord.setCrucialTimeList(crucialTimeLists);
            }
        }
        //his同步到的检查数据
        List<CrucialRecordResponse> examRecords = examReportDAO.queryExamReportInfo(qo);
        if (CommonUtils.isNotEmpty(examRecords)) {
            for (CrucialRecordResponse exam : examRecords) {
                List<CrucialTimeRecordList> crucialTimeLists = examReportDAO.queryExamReportTimeInfo(qo, exam.getIndexType());
                List<CrucialTimeRecordList> collect = crucialTimeLists.stream().map(crucial -> {
                    if (CommonUtils.isEmpty(crucial.getIndexValue())) {
                        crucial.setIndexValue("");
                    }
                    return crucial;
                }).collect(Collectors.toList());
                exam.setCrucialTimeList(collect);
            }
        }
        //编辑器同步的检验信息
        List<CrucialRecordResponse> crucialRecordResponses1 = fusAnswerInspectionReportInfoDAO.queryAnswerInspectionReportInfo(qo);
        if (CommonUtils.isNotEmpty(crucialRecordResponses1)) {
            for (CrucialRecordResponse recordResponse : crucialRecordResponses1) {
                if (CommonUtils.isNotEmpty(recordResponse.getIndexName())) {
                    List<CrucialTimeRecordList> crucialTimeLists = fusAnswerInspectionReportInfoDAO.queryAnswerInspectionReportTimeInfo(qo, recordResponse.getIndexType());
                    recordResponse.setCrucialTimeList(crucialTimeLists);
                }
            }
        }
        //编辑器同步的检查信息
        List<CrucialRecordResponse> crucialAnswerExamList = fusAnswerCheckReportInfoDAO.queryAnswerExamReportInfo(qo);
        if (CommonUtils.isNotEmpty(crucialAnswerExamList)) {
            for (CrucialRecordResponse crucialRecord : crucialAnswerExamList) {
                if (CommonUtils.isNotEmpty(crucialRecord.getIndexName())) {
                    List<CrucialTimeRecordList> crucialTimeLists = fusAnswerCheckReportInfoDAO.queryAnswerExamReportTimeInfo(qo, crucialRecord.getIndexName());
                    crucialRecord.setCrucialTimeList(crucialTimeLists);
                }
            }
        }

        //病种配置的关联指标信息
        List<CrucialRecordResponse> crucialRecordResponses2 = diseaseSpeciesRelateDAO.queryRelateInspectionReportInfo(qo);
        //合并关联指标
        List<CrucialRecordResponse> responseList = new ArrayList<>();
        //病种配置的关联指标才展示在患者视图
        if (CommonUtils.isEmpty(crucialRecordResponses2)) {
            return responseList;
        }
        for (CrucialRecordResponse crucialRecord2 : crucialRecordResponses2) {
            //健康监测同步的数据
            if (CommonUtils.isNotEmpty(crucialRecordResponses)) {
                for (CrucialRecordResponse crucialRecord : crucialRecordResponses) {
                    if (CommonUtils.isNotEmpty(crucialRecord2.getIndexHpiCode())) {
                        if (crucialRecord.getIndexType().equals(crucialRecord2.getIndexHpiCode())) {
                            crucialRecord2.setCrucialType("1");
                            crucialRecord2.setCrucialTimeList(crucialRecord.getCrucialTimeList());
                            //responseList.add(crucialRecord2);
                        }
                    }
                }
            }
            //同步编辑器的检验数据
            if (CommonUtils.isNotEmpty(crucialRecordResponses1)) {
                for (CrucialRecordResponse crucialRecordResponse : crucialRecordResponses1) {
                    if (CommonUtils.isNotEmpty(crucialRecordResponse.getIndexName())) {
                        if (crucialRecordResponse.getIndexName().equals(crucialRecord2.getIndexName())) {
                            //crucialRecord2.setCrucialType("1");
                            crucialRecord2.setCrucialTimeList(crucialRecordResponse.getCrucialTimeList());
                            //responseList.add(crucialRecord2);
                        }
                    }
                }
            }
            //同步编辑器的检查数据
            if (CommonUtils.isNotEmpty(crucialAnswerExamList)) {
                for (CrucialRecordResponse crucialRecordResponse : crucialAnswerExamList) {
                    if (CommonUtils.isNotEmpty(crucialRecordResponse.getIndexName())) {
                        if (crucialRecordResponse.getIndexName().equals(crucialRecord2.getIndexName())) {
                            crucialRecord2.setCrucialType("2");
                            crucialRecord2.setCrucialTimeList(crucialRecordResponse.getCrucialTimeList());
                            //responseList.add(crucialRecord2);
                        }
                    }
                }
            }
            //同步检验数据
            if (CommonUtils.isNotEmpty(crucialRecordResponses3)) {
                for (CrucialRecordResponse crucialRecordResponse : crucialRecordResponses3) {
                    if (StringUtils.isNotEmpty(crucialRecordResponse.getIndexName()) && crucialRecordResponse.getIndexName().equals(crucialRecord2.getIndexName())) {
                        crucialRecord2.setCrucialType("1");
                        crucialRecord2.setCrucialTimeList(crucialRecordResponse.getCrucialTimeList());
                        //responseList.add(crucialRecord2);
                    }
                }
            }
            //同步检查数据
            if (CommonUtils.isNotEmpty(examRecords)) {
                for (CrucialRecordResponse crucialRecordResponse : examRecords) {
                    if (StringUtils.isNotEmpty(crucialRecordResponse.getIndexName()) && crucialRecordResponse.getIndexName().equals(crucialRecord2.getIndexName())) {
                        crucialRecord2.setCrucialType("2");
                        crucialRecord2.setCrucialTimeList(crucialRecordResponse.getCrucialTimeList());
                        //responseList.add(crucialRecord2);
                    }
                }
            }
        }
        //合并相同的关联指标信息
//        List<CrucialRecordResponse> result = responseList.stream()
//                // 表示name为key，接着如果有重复的，那么从MedicalRecordResponse对象o1与o2中筛选出一个，这里选择o1，
//                .collect(Collectors.toMap(CrucialRecordResponse::getIndexName, a -> a, (o1,o2)-> {
//                    o1.getCrucialTimeList().addAll(o2.getCrucialTimeList());
//                    return o1;
//                })).values().stream().collect(Collectors.toList());
        List<CrucialRecordResponse> collect = crucialRecordResponses2.stream().map(crucial -> {
            List<CrucialTimeRecordList> timeList = new ArrayList<>();
            if (CommonUtils.isEmpty(crucial.getCrucialTimeList())) {
                crucial.setCrucialTimeList(timeList);
            }
            return crucial;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 随访记录获取
     *
     * @param qo
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public Map<String, List<FollowUpRecordsResponse>> queryFollowUpRecord(GroupChronicRecordQo qo) throws ControllerException {
        Map<String, List<FollowUpRecordsResponse>> resultMap = new HashMap<>();
        if (CommonUtils.isEmpty(qo.getPatientId())) {
            throw new ControllerException("患者唯一标识不可为空");
        }
        if (CommonUtils.isEmpty(qo.getDiseaseTypeId())) {
            throw new ControllerException("病种id不可为空");
        }
        if (CommonUtils.isEmpty(qo.getBeginTime())) {
            throw new ControllerException("开始时间不能为空");
        }
        if (CommonUtils.isEmpty(qo.getEndTime())) {
            throw new ControllerException("结束时间不能为空");
        }
        List<FollowUpRecordsResponse> followUpRecordsResponses = followUpRecordsDAO.queryFollowUpRecordsInfoList(qo);
        if (CommonUtils.isNotEmpty(followUpRecordsResponses)) {
            resultMap.put("reviewConsultList",
                    followUpRecordsResponses.stream().filter(f -> Objects.equals(f.getRouteDetailType(), "1")).collect(Collectors.toList()));
            resultMap.put("groupAssessList",
                    followUpRecordsResponses.stream().filter(f -> Objects.equals(f.getRouteDetailType(), "2")).collect(Collectors.toList()));
            resultMap.put("followQuestionList",
                    followUpRecordsResponses.stream().filter(f -> Objects.equals(f.getRouteDetailType(), "3")).collect(Collectors.toList()));
            resultMap.put("examCruicalList",
                    followUpRecordsResponses.stream().filter(f -> Objects.equals(f.getRouteDetailType(), "4")).collect(Collectors.toList()));
            resultMap.put("followMedicalCareList",
                    followUpRecordsResponses.stream().filter(f -> Objects.equals(f.getRouteDetailType(), "5")).collect(Collectors.toList()));
            resultMap.put("healthEduList",
                    followUpRecordsResponses.stream().filter(f -> Objects.equals(f.getRouteDetailType(), "6")).collect(Collectors.toList()));
            resultMap.put("hmList",
                    followUpRecordsResponses.stream().filter(f -> Objects.equals(f.getRouteDetailType(), "7")).collect(Collectors.toList()));
        }
        return resultMap;
    }

    /**
     * 根据病种id和扩展属性id获取属性值
     *
     * @param qo
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public List<Map<String, Object>> queryDiseaseExtendProperty(GroupChronicDeleteRequest qo) throws
            ControllerException {
        List<Map<String, Object>> resultList = new ArrayList<>();
        if (CommonUtils.isEmpty(qo.getDiseaseTypeId())) {
            throw new ControllerException("病种id不可为空");
        }
        if (CommonUtils.isEmpty(qo.getExtendId())) {
            throw new ControllerException("扩展属性id不可为空");
        }
        List<DiseaseExtendPropertyEntity> secondList = diseaseExtendPropertyDAO.querySecondDiseaseByDiseaseTypeId(qo.getDiseaseTypeId(), qo.getExtendId());
        if (CommonUtils.isNotEmpty(secondList)) {
            for (DiseaseExtendPropertyEntity diseaseExtendProperty : secondList) {
                Map<String, Object> map = new HashMap<>();
                map.put("extendId", diseaseExtendProperty.getExtendId());
                map.put("extendName", diseaseExtendProperty.getName());
                map.put("viewDispFlag", diseaseExtendProperty.getViewDispFlag());
                map.put("optGroupFlag", diseaseExtendProperty.getOptGroupFlag());
                map.put("createTime", diseaseExtendProperty.getCreateTime());
                resultList.add(map);
            }
        }
        return resultList;
    }

}
